package dnl.util.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

import ajul.lang.Args;

/**
 * @author daniel
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class WindowUtils {

	public enum Corner {
		TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
	};
	
	public enum Orientation {
		VERTICAL, HORIZONTAL
	};

	public static void fitWindowToScreen(Window w) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		w.setSize(d.width, d.height);
	}

	public static void centerWindow(Window w) {
		setWindowLocation(w, .5, .5);
	}

	public static void centerWindowRelative(Window w, Window relativeWindow) {
		Args.assertNotNull(w);
		Args.assertNotNull(relativeWindow);
		centerWindowRelative(w, (Component) relativeWindow);
	}

	public static void centerWindowRelative(Window w, Component relativeComponent) {
		Rectangle r = relativeComponent.getBounds();
		int x = (int) (r.width * .5 - w.getBounds().width * .5) + r.x;
		int y = (int) (r.height * .5 - w.getBounds().height * .5) + r.y;
		w.setLocation(x, y);
	}

	public static void setRelativeLocation(Window w, Window relativeWindow, double xLoc, double yLoc) {
		Rectangle r = relativeWindow.getBounds();
		int x = (int) (r.width * xLoc - w.getBounds().width * .5) + r.x;
		int y = (int) (r.height * yLoc - w.getBounds().height * .5) + r.y;
		w.setLocation(x, y);
	}

	public static void setWindowLocation(Window w, double xLoc, double yLoc) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (d.width * xLoc - w.getBounds().width * xLoc);
		int y = (int) (d.height * yLoc - w.getBounds().height * yLoc);
		w.setLocation(x, y);
	}

	public static void setWindowLocation(Window w, Corner corner) {
		setWindowLocation(w, corner, false);
	}
	
	/**
	 * Locate a given window at one of the screen's corners.
	 * 
	 * @param w
	 *            a <code>Window</code> instance.
	 * @param corner
	 *            the <code>Corner</code> in which to locate the window.
	 */
	public static void setWindowLocation(Window w, Corner corner, boolean ignoreTaskbars) {
		Rectangle r = getWorkarea(ignoreTaskbars);

		switch (corner) {
		case TOP_LEFT:
			w.setLocation(0, 0);
			break;
		case TOP_RIGHT:
			w.setLocation(r.width - w.getBounds().width, 0);
			break;
		case BOTTOM_LEFT:
			w.setLocation(0, r.height - w.getBounds().height);
			break;
		case BOTTOM_RIGHT:
			w.setLocation(r.width - w.getBounds().width, r.height - w.getBounds().height);
			break;
		}
	}
	
	/**
	 * 
	 * @param w
	 * @param location
	 * @param ignoreTaskbars
	 */
	public static void setLocationClosest(Component w, Point location, boolean ignoreTaskbars) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = location.x, y = location.y;
		if (location.y + w.getHeight() > screenSize.height) {
			y = screenSize.height - w.getHeight();
		}
		if (location.x + w.getWidth() > screenSize.width) {
			x = screenSize.width - w.getWidth();
		}
		w.setLocation(x, y);
	}

	private static Rectangle getWorkarea(boolean ignoreTaskbars) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle r = new Rectangle(0, 0, d.width, d.height);
		if(!ignoreTaskbars){
			GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
			r = graphicsEnvironment.getMaximumWindowBounds();
		}
		return r;
	}

	public static void setWindowSize(Window w, double xSize, double ySize) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (d.width * xSize);
		int y = (int) (d.height * ySize);
		w.setSize(x, y);
	}
	
	public static void cascadeWindows(Orientation orientation, Window... windows) {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int n = windows.length;
		int w = d.width;
		int h = d.height;
		switch (orientation) {
			case VERTICAL:
				w = (w / n);
				break;
			case HORIZONTAL:
				h = (h / n);
				break;
		}

		for (int i = 0; i < windows.length; i++) {
			Window window = windows[i];
			window.setSize(w, h);
			switch (orientation) {
				case VERTICAL:
					window.setLocation(i * w, 0);
					break;
				case HORIZONTAL:
					window.setLocation(0, i*h);
					break;
			}
		}
	}
}
